package com.zinnaworks.smart_cloud_gamepad;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.telephony.TelephonyManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class IntroActivity extends Activity {
	
	Handler h;
	
	RelativeLayout popBg;
	final Context context = this;
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(android.os.Build.VERSION.SDK_INT > 9){
			ThreadPolicy policy = new ThreadPolicy.Builder().permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.activity_intro);
        popBg = (RelativeLayout) findViewById(R.id.popBG);
      
        
        h = new Handler();
        h.postDelayed(irun, 3000);
        
    }
    
    private boolean checkItem()
    {
    	
    	if(!isServiceSKTelecom())
    	{
    		//통신사 팝업 and 종료
    		popBg.setVisibility(View.VISIBLE);
    		alert2LineDialog("Cloud Gamepad 종료","Cloud Gamepad APP은\nSKT가입자만 사용 가능합니다.");
    		return false;
    	}
    	
    	if(!isConnectTypeWifi())
    	{
    		//와이파이 연결팝업
    		popBg.setVisibility(View.VISIBLE);
    		alert3LineDialog("Cloud Gamepad 종료","네트워크에 연결되어있지 않습니다.\n네트워크 연결  상태 확인 후\n다시 이용해주세요.");
    		return false;
    	}
    	return true;
    }

    private void alert2LineDialog(String tt ,String msg)
    {
    	
    	final Dialog dialog = new Dialog(context ,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    	dialog.setContentView(R.layout.popup_layout2);
    	TextView title = (TextView) dialog.findViewById(R.id.popup_title);
    	title.setText(tt);
    	
    	TextView detailText = (TextView) dialog.findViewById(R.id.popup_content);
    	detailText.setText(msg);
    	
    	final Button dialogButton = (Button) dialog.findViewById(R.id.okBtn);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popBg.setVisibility(View.GONE);
    			exitMehtod();
			}
		});
		dialogButton.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
				// TODO Auto-generated method stub
				switch (paramMotionEvent.getAction()) {
				case MotionEvent.ACTION_DOWN:
					dialogButton.setTextColor(Color.parseColor("#ffffff"));
					break;
				case MotionEvent.ACTION_UP:
					dialogButton.setTextColor(Color.parseColor("#4f5051"));
					break;
				}
				return false;
			}
		});
		dialog.show();
    	

    }
    
    private void alert3LineDialog(String tt ,String msg)
    {
    	
    	final Dialog dialog = new Dialog(context ,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    	dialog.setContentView(R.layout.popup_layout_3_line);
    	TextView title = (TextView) dialog.findViewById(R.id.popup_title);
    	title.setText(tt);
    	
    	TextView detailText = (TextView) dialog.findViewById(R.id.popup_content);
    	detailText.setText(msg);
    	
    	final Button dialogButton = (Button) dialog.findViewById(R.id.okBtn);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popBg.setVisibility(View.GONE);
    			exitMehtod();
			}
		});
		dialogButton.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
				// TODO Auto-generated method stub
				switch (paramMotionEvent.getAction()) {
				case MotionEvent.ACTION_DOWN:
					dialogButton.setTextColor(Color.parseColor("#ffffff"));
					break;
				case MotionEvent.ACTION_UP:
					dialogButton.setTextColor(Color.parseColor("#4f5051"));
					break;
				}
				return false;
			}
		});
		dialog.show();
    	

    }

    public void exitMehtod()
    {
//    	moveTaskToBack(true);
//		finish();
//		android.os.Process.killProcess(android.os.Process.myPid());
    	android.os.Process.killProcess(android.os.Process.myPid());
    	super.onDestroy();
    }
    
    //통신사 체크 
    private boolean isServiceSKTelecom()
    {
    	boolean isBoolean = false;
    	// Get System TELEPHONY service reference
    	TelephonyManager tManager = (TelephonyManager) getBaseContext()
    			.getSystemService(Context.TELEPHONY_SERVICE);

    	// Get carrier name (Network Operator Name)
    	String carrierName = tManager.getNetworkOperatorName();
    	
    	if(carrierName.equalsIgnoreCase("SKTelecom"))
    		isBoolean = true;
    	
    	return isBoolean;
    }
    
    //와이파이 사용유무 체크
    private boolean isConnectTypeWifi()
    {
    	boolean isConnected = false;
    	ConnectivityManager cManager; 
    	NetworkInfo wifi; 

    	cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
    	wifi = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    	if( wifi.isConnected())
    		isConnected = true;

    	return isConnected;
    }
    
    Runnable irun = new Runnable() {
		
		@Override
		public void run() {
			if(checkItem())
			{
				Intent i = new Intent(IntroActivity.this, ConnectGamepad.class);
				startActivity(i);
				finish();
				
				overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			}
			
		}
	};
	
	@Override
	public void onBackPressed(){
		super.onBackPressed();
		h.removeCallbacks(irun);
		
	}
	
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		switch (keyCode) {
//
//		//하드웨어 뒤로가기 버튼에 따른 이벤트 설정
//		case KeyEvent.KEYCODE_BACK:
//		{
//			ExitDialogPopup exit = new ExitDialogPopup(this);
//			exit.show();
////			
//			break;
//		}
//			
//		default:
//			break;
//		}
//
//		return super.onKeyDown(keyCode, event);
//	}
}
